package com.reggie.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.dto.CategoryDTO;
import com.reggie.dto.CategoryPageQueryDTO;
import com.reggie.entity.Category;
import com.reggie.exception.DeletionNotAllowedException;
import com.reggie.mapper.CategoryMapper;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.result.PageResult;

import com.reggie.result.R;
import com.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //添加分类
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //设置默认禁用状态
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.save(category);
    }

    //分页查询
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO pageQueryDTO) {
        //利用分页工具 传入分页页码, 和页码记录数
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(pageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //删除分类
    @Override
    public void deleteById(Long id) {
        //查询当前id下的关联菜品数量
        Integer count = dishMapper.countByCategoryId(id);
        if (count > 0) {
            //说明关联的有菜品不能删除 抛出自定义异常
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //根据分类获取当前id对应的套餐数量
        count = setmealMapper.countByCategoryId(id);
        if (count > 0) {
            //说明关联的有套餐  也不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //如果以上两个判断条件不成立 就正常删除
        categoryMapper.deleteById(id);
    }

    //修改分类
    @Override
    public void updateById(CategoryDTO categoryDTO) {
        //根据id修改分类
        //获取一个category对象
        Category category = new Category();
        //categoryDTO 只包含了 主键 分类(1/2) 分类名称  排序  所以复制一下 给category对象
        BeanUtils.copyProperties(categoryDTO, category);
        log.info(category.toString());
        categoryMapper.update(category);
    }

    //启用 禁用
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder().status(status).id(id).build();
        categoryMapper.startOrStop(category);
    }
    //查询菜品管理下的资源
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }


}
